﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Restaurant.Controls
{
    public partial class SingleTableUserControl : UserControl
    {
        private DateTime _lastChangeTime;

        #region Properties

        public string TableBackColor
        {
            set
            {
                var colorConv = new ColorConverter();
                BackColor = (Color)colorConv.ConvertFromInvariantString(value);
            }
        }

        public string TableNameColor
        {
            set
            {
                var colorConv = new ColorConverter();
                nameLabelControl.ForeColor = (Color)colorConv.ConvertFromInvariantString(value);
                initialTimeEdit.ForeColor = (Color)colorConv.ConvertFromInvariantString(value);
                lastChangeTimeEdit.ForeColor = (Color)colorConv.ConvertFromInvariantString(value);
                stageLabelControl.ForeColor = (Color)colorConv.ConvertFromInvariantString(value);
                valueCalcEdit.ForeColor = (Color)colorConv.ConvertFromInvariantString(value);
            }            
        }

        public string TableName
        {
            set { nameLabelControl.Text = value; }
            get { return nameLabelControl.Text; }
        }

        public int Places { set; get; }

        public long TableId { set; get; }

        public long? RequestId { set; get; }

        #endregion


        public SingleTableUserControl()
        {
            InitializeComponent();
            SetControlsVisibility();

        }

        private void SetControlsVisibility()
        {
            initialTimeEdit.Visible = RequestId.HasValue;
            lastChangeTimeEdit.Visible = RequestId.HasValue;
            stageLabelControl.Visible = RequestId.HasValue;
            valueCalcEdit.Visible = RequestId.HasValue;
        }

        public void RefreshTime(DateTime time)
        {
            var timeSpan = time.Subtract(_lastChangeTime);            
            lastChangeTimeEdit.EditValue = new DateTime(time.Year, time.Month, time.Day, timeSpan.Hours, timeSpan.Minutes, 0);
        }

        public void Free()
        {
            RequestId = null;
            SetControlsVisibility();
        }

        public void Ocuppy(long requestId, DateTime initialTime, DateTime lastChangeTime, string stage, decimal value)
        {
            RequestId = requestId;
            SetControlsVisibility();            
            initialTimeEdit.EditValue = initialTime;
            _lastChangeTime = lastChangeTime;
            RefreshTime(lastChangeTime);            
            stageLabelControl.Text = stage;
            valueCalcEdit.EditValue = value;            
        }
    }
}
